18. Choosing good data structures

Choosing Good Data Structures

Lists

Lists are good when:

  1. Your data is ordered (and indexing based on position-in-list makes sense).
  2. You need to keep track of duplicates.
  3. Your data is mutable (dictionary keys and elements in a set must be immutable).

Sets

Sets are good for:

  1. Fast membership testing.
  2. Removing duplicates from a sequence.
  3. Computing mathematical operations like intersection, union, and difference.

Dictionaries

Dictionaries are good when:

  1. You want to associate keys with values.
  2. You want fast key-based lookups.

What data structure would be best for representing a one-dimensional grid world (as you did in Bayesian Thinking)?

SOLUTION: List

You want to associate a population with every big city in your country. What data structure would be best for storing this information?

SOLUTION: Dictionary

You want your self driving car to visit every city in your country. You don't care how many times it visits any given city. What data structure would be best for storing the names of all the cities your car has visited?

SOLUTION: Set